package org.aristotle.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.user.model.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import org.aristotle.user.model.entity.User;
import org.aristotle.user.service.IUserService;
import org.aristotle.user.model.vo.UserVO;

@RestController
@RequestMapping("user")
@Api(value="用户controller",tags={"用户操作接口"})
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "用户id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "username", paramType = "query", value = "用户名", dataType = "String", required = false),
            @ApiImplicitParam(name = "nickname", paramType = "query", value = "昵称", dataType = "String", required = false),
            @ApiImplicitParam(name = "status", paramType = "query", value = "状态", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<UserVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "username",required = false) String username,
            @RequestParam(name = "nickname",required = false) String nickname,
            @RequestParam(name = "status",required = false) Integer status,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<UserVO> result = userService.queryPageList(
            id,
            username,
            nickname,
            status,
            current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "用户id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        UserVO result = userService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(userService.list(),UserVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody UserVO userVO) {
        User user = AristotleBeanUtils.transitionBean(userVO, User::new);
        user.setId(null);
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        userService.save(user);
        return ResponseEntity.addOk(userVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody UserVO userVO) {
        if (userVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        User user = AristotleBeanUtils.transitionBean(userVO, User::new);
        user.setPassword(null);
        userService.updateById(user);
        return ResponseEntity.updateOk(userVO);
    }

    @PutMapping("password")
    @ApiOperation("修改密码")
    public Result<?> editPassword(@RequestBody UserVO userVO) {
        if (userVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        User user = AristotleBeanUtils.transitionBean(userVO, User::new);

        String password = passwordEncoder.encode(user.getPassword());
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",user.getId());
        wrapper.set("password",password);
        if (userService.update(wrapper)) {
            return ResponseEntity.updateOk(userVO);
        } else {
            return ResponseEntity.error();
        }

    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "用户id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        userService.removeById(id);
        return ResponseEntity.deleteOk(id);
    }

    @GetMapping("getByUsername")
    @ApiOperation("根据用户名获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", paramType = "query", value = "用户名", dataType = "String", required = true)
    })
    public Result<?> getUserByUsername(@RequestParam("username") String username) {
        if (StringUtils.isBlank(username)) {
            return ResponseEntity.badRequest();
        }
        UserDTO result = userService.getByUsername(username);
        return ResponseEntity.ok(result);
    }

}
